package cn.lyjuan.android.mylibrary.view.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by chad on 18-3-13.
 */

public abstract class AbstractFragment extends Fragment
{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        restoreFromArgs();

        return null;
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

        storeToArgs();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();

        storeToArgs();
    }

    /**
     * 将数据保存到arguments中
     */
    protected void storeToArgs()
    {
        Bundle ble = getArguments();
        if (null == ble)
            throw new RuntimeException("arguments cannot be null");

        store(ble);
    }

    /**
     * 存储数据
     *
     * @param ble
     */
    protected abstract void store(Bundle ble);


    /**
     * 交需要的数据恢复到arguments中
     */
    protected void restoreFromArgs()
    {
        Bundle ble = getArguments();
        if (null == ble)
            throw new RuntimeException("arguments cannot be null");

        restore(ble);
    }

    /**
     * 恢复数据
     *
     * @param ble
     */
    protected abstract void restore(Bundle ble);
}
